use axum::Json;
use serde_json::Value;
use sea_orm::{ query::*, sea_query::Expr, DbErr, DeleteResult, EntityTrait, FromQueryResult };
use crate::{ code, file::remove::remove_files_rel, query, sea_orm::{connect, sql::sql_trait::{SqlId, SqlIn}}};
use serde::{ Deserialize, Serialize };

#[derive(Debug, Deserialize, Serialize, FromQueryResult)]
pub struct Data {}

// 删除单个
pub async fn del<E:EntityTrait, T:SqlId>(entity:E, field:&str, value:T, file_names: Option<Vec<&str>>) -> Result<Json<Value>, Json<Value>>{
    let conn = connect().await?;
    let table = entity.table_name();

    let sql = format!("delete from {} where {}={}", table, &field, value.val());

    let files = super::delete_file::index(field, value, file_names, &conn, table, true).await;
    if let Some(f) = files { _ = remove_files_rel(f).await?; }

    let stmt = Statement::from_string(conn.get_database_backend(), sql );
    let rs= conn.execute(stmt).await;
    code::send_ok(rs)
}
// 删除多个
pub async fn dels<E:EntityTrait, T:SqlIn>(entity:E, field:&str, value:T, file_names: Option<Vec<&str>>) -> Result<Json<Value>, Json<Value>>{
    let conn = connect().await?;
    let sql = value.val(field);
    
    let files = super::delete_files::index(field, value, file_names, &conn, entity.table_name(), false).await;
    if let Some(f) = files {_ = remove_files_rel(f).await?; }
    
    let result: Result<DeleteResult,DbErr> = Delete::many(entity)
    .filter(Expr::cust( sql ))
    .exec(&conn).await;
    code::send_ok(result)
}
// 删除单个
pub async fn by_id<E:EntityTrait, T:SqlId>(entity:E, value:T, file_names: Option<Vec<&str>> ) -> Result<Json<Value>, Json<Value>>{ del(entity, "id", value, file_names).await }
pub async fn by_uuid<E:EntityTrait, T:SqlId>(entity:E, value:T, file_names: Option<Vec<&str>> ) -> Result<Json<Value>, Json<Value>>{ del(entity, "uuid", value, file_names).await }
pub async fn by_no<E:EntityTrait, T:SqlId>(entity:E, value:T, file_names: Option<Vec<&str>> ) -> Result<Json<Value>, Json<Value>>{ del(entity, "no", value, file_names).await }
// 删除多个
pub async fn by_names<E:EntityTrait>(entity:E, field:&str, value:&str, file_names: Option<Vec<&str>>) -> Result<Json<Value>, Json<Value>>{
    let num = query::utils::str_to_vec_u32_op(&value)?;
    let str = query::utils::str_to_vec_str_op(&value)?;

    if let Some(f) = num {
        if f.len() == 0 {
            del(entity, &field, f[0], file_names).await
        }else{
            dels(entity, field, f, file_names).await
        }
       
    }else{
        if let Some(f) = str {
            if f.len() == 0 {
                del(entity, &field, f[0].to_owned(), file_names).await
            }else{
                dels(entity, field, f, file_names).await
            }
        }else{
            code::sys("删除参数不存在")
        }
    }
}
pub async fn by_ids<E:EntityTrait>(entity:E, value:&str, file_names: Option<Vec<&str>>) -> Result<Json<Value>, Json<Value>>{ by_names(entity,"id", value,file_names ).await }
pub async fn by_uuids<E:EntityTrait>(entity:E, value:&str, file_names: Option<Vec<&str>>) -> Result<Json<Value>, Json<Value>>{ by_names(entity,"uuid", value,file_names ).await }
pub async fn by_nos<E:EntityTrait>(entity:E, value:&str, file_names: Option<Vec<&str>>) -> Result<Json<Value>, Json<Value>>{ by_names(entity,"no", value,file_names ).await }